package com.xu.mall.base.tree;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import com.xu.mall.base.tree.entity.ITreeEntity;

public abstract class BaseTree<T, PK extends Serializable> implements ITreeNode<PK> {
	private ITreeEntity node;

	public ITreeEntity getNodeDate() {
		return node;
	}

	// public ITree<PK> getParent();

	// public ITree<PK> getRight();

	// public ITree<PK> getLeft();

	// public Set<ITree<PK>> getChildren();

	public Set<ITreeNode<PK>> getAllChildren() {
		Set<ITreeNode<PK>> children = new HashSet<ITreeNode<PK>>();
		if (hasChildren()) {
			for (ITreeNode<PK> child : getChildren()) {
				children.addAll(child.getAllChildren());
			}
		}
		return children;
	}

	public int getChildrenCount() {
		if (getChildren() == null) {
			return 0;
		}
		return getChildren().size();
	}

	public int getAllChildrenCount() {
		if (getAllChildren() == null) {
			return 0;
		}
		return getAllChildren().size();
	}

	public boolean hasParent() {
		return getParent() == null ? false : true;
	}

	public boolean hasChildren() {
		if (getChildren() == null || getChildren().size() == 0) {
			return false;
		}
		return true;
	}

	public boolean isParentOf(PK id) {
		if (hasChildren()) {
			for (ITreeNode<PK> tree : getChildren()) {
				if (id.equals(tree.getNodeDate().getId())) {
					return true;
				}
			}
		}
		return false;
	}

	public boolean isChildOf(PK id) {
		if (hasParent()) {
			id.equals(getParent().getNodeDate().getId());
		}
		return false;
	}
}
